#define BOOST_TEST_MODULE testElementRuleState

#include <boost/test/unit_test.hpp>

#include "common/elements/element_maker.h"
#include "common/elements/rule_state.h"

BOOST_AUTO_TEST_CASE(testRuleStateJson)
{
    auto first = std::make_shared<Elements::RuleState>();
    first->setId(1);
    first->setRuleId(2);
    first->setStateId(3);

    {
        Elements::Permissions permissions;
        permissions.setFlag(Elements::Permission::Read);
        permissions.setFlag(Elements::Permission::Write);
        first->setPermissions(permissions);
    }

    const auto json = first->toJsonString();
    const Elements::ElementPtr second(Elements::fromJsonString(json));
    BOOST_CHECK(first->isSame(second.get()));

    const auto third = std::static_pointer_cast<Elements::RuleState>(second);
    BOOST_CHECK(1 == third->id());
    BOOST_CHECK(2 == third->ruleId());
    BOOST_CHECK(3 == third->stateId());

    {
        const auto permissions = third->permissions();
        BOOST_CHECK(false == permissions.testFlag(Elements::Permission::None));
        BOOST_CHECK(true == permissions.testFlag(Elements::Permission::Read));
        BOOST_CHECK(true == permissions.testFlag(Elements::Permission::Write));
    }
}

BOOST_AUTO_TEST_CASE(testRulePermissions)
{
    {
        Elements::Permissions permissions;
        BOOST_CHECK(true == permissions.testFlag(Elements::Permission::None));
        BOOST_CHECK(false == permissions.testFlag(Elements::Permission::Read));
        BOOST_CHECK(false == permissions.testFlag(Elements::Permission::Write));
    }

    {
        Elements::Permissions permissions;
        permissions.setFlag(Elements::Permission::Read);
        BOOST_CHECK(false == permissions.testFlag(Elements::Permission::None));
        BOOST_CHECK(true == permissions.testFlag(Elements::Permission::Read));
        BOOST_CHECK(false == permissions.testFlag(Elements::Permission::Write));
    }

    {
        Elements::Permissions permissions;
        permissions.setFlag(Elements::Permission::Read);
        permissions.setFlag(Elements::Permission::Write);
        BOOST_CHECK(false == permissions.testFlag(Elements::Permission::None));
        BOOST_CHECK(true == permissions.testFlag(Elements::Permission::Read));
        BOOST_CHECK(true == permissions.testFlag(Elements::Permission::Write));
    }
}
